#!/user/bin/env python3
# -*- coding: utf-8 -*-

import heapq
import itertools
from collections import Counter

"""
堆排序
    从列表中找出最大或最小的N个元素
"""


def heap_sort():
    list1 = [34, 25, 12, 99, 80, 44, 53, 46, 23, 52]
    list2 = [
        {'name': 'IBM', 'shares': 100, 'price': 91.1},
        {'name': 'AAP', 'shares': 50, 'price': 543.22},
        {'name': 'FB', 'shares': 200, 'price': 21.1},
        {'name': 'HPQ', 'shares': 35, 'price': 31.75},
        {'name': 'Yahoo', 'shares': 45, 'price': 16.35},
        {'name': 'ACME', 'shares': 75, 'price': 115.65}
    ]
    print(heapq.nlargest(3, list1))
    print(heapq.nsmallest(3, list1))
    print(heapq.nsmallest(2, list2, key=lambda x: x['price']))
    print(heapq.nlargest(2, list2, key=lambda x: x['shares']))


# heap_sort()


def compute_tool():
    # ABCD的全排列
    itertools.permutations('ABCD')
    # ABCDE五选三组合
    itertools.combinations('ABCDE', 3)
    # ABCD与123的笛卡尔积
    itertools.product('ABCD', '123')
    # ABC的无线循环序列
    itertools.cycle(('A', 'B', 'C'))


# compute_tool()


"""
collections中常用的工具：
    1.namedtuple:它是一个类工厂，介绍的后类型的名称和属性列表来创建一个类。
    2.deque：双端队列，列表基于数字，deque基于双向链表
    3.Counter：是dict的子类，其中键为元素，值为元素的计数，most_common获取频率最高的元素
    4.OrderedDict:记录了键值对插入的顺序
    5.defaultdict:可以通过默认的工厂函数来获得键对应的默认值
"""


def most_words():
    # 统计出现次数最多的元素
    words = [
        'look', 'into', 'my', 'eyes', 'look', 'into', 'my', 'eyes',
        'the', 'eyes', 'the', 'eyes', 'the', 'eyes', 'not', 'around',
        'the', 'eyes', "don't", 'look', 'around', 'the', 'eyes',
        'look', 'into', 'my', 'eyes', "you're", 'under'
    ]
    counter = Counter(words)
    print(counter.most_common(3))


# most_words()
